package com.fw.application.controller.base;

import com.fw.application.security.comm.BaseUserDetails;
import com.fw.enums.CodeStatus;
import com.fw.exception.CustomException;
import com.fw.system.web.model.entity.TbUser;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author guohaibin
 */
public interface UserController {


    /**
     * h获取系统用户
     * @return TbUser
     */
    default TbUser getUser(){
        try {
            return ((BaseUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
        }catch (ClassCastException e){
            e.printStackTrace();
        }
        throw new CustomException(CodeStatus.SIGN_ERROR.getMessage());
    }

    /**
     * 容灾机制，用来获取默认用户。
     * @return
     */
    default TbUser getDefaultUser(){
        try {
            return ((BaseUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getDefaultUser();
        }catch (Exception e){
            return null;
        }
    }
}
